package com.fengkai.filepost.pdfoutstream.test.ODsolutions;

import java.util.*;

/**
 * @author Fengkai
 * @creat 2023/4/17
 * 输入
 * 手上已有的牌
 * 已经出过的牌(包括对手出的和自己出的牌)
 * 输出：对手可能构成的最长的顺子(如果有相同长度的顺子，输出牌面最大的那一个)，
 * 如果无法构成顺子，则输出NO-CHAIN
 * 输入
 * 输入的第一行为当前手中的牌
 * 输入的第一行为已经出过的牌
 * 输出
 * 最长的顺子
 */
public class DouDiZhu {
    static HashMap<Integer, Integer> keys = new HashMap<Integer, Integer>();

    static void initMap () {
        keys.clear();
        keys.put(3, 4);
        keys.put(4, 4);
        keys.put(5, 4);
        keys.put(6, 4);
        keys.put(7, 4);
        keys.put(8, 4);
        keys.put(9, 4);
        keys.put(10, 4);
        keys.put(11, 4);//J
        keys.put(12, 4);//Q
        keys.put(13, 4);//K
        keys.put(14, 4);//A
    }

    public static void main (String[] args) {
        System.out.println(getShunZi("3-3-3-3-8-8-8-8", "K-K-K-K"));
    }

    public static String getShunZi (String myHand, String allPol) {
        initMap();
        reduceCard(myHand);
        reduceCard(allPol);
        LinkedList<Integer> cards = new LinkedList<>();
        int length = 1;
        int max = 1;
        String result = "";
        StringBuilder cacheString = new StringBuilder();
        for (Map.Entry<Integer, Integer> entry : keys.entrySet()) {
            if (entry.getValue() > 0) {
                cards.addLast(entry.getKey());
            }
        }
        while (cards.size() > 1) {
            int pre = cards.removeFirst();
            Integer peek = cards.getFirst();
            if (pre + 1 == peek){
                length++;
                if (length == 2){
                    String cache = getString(pre);
                    String string = getString(peek);
                    cacheString.append(cache).append("-").append(string).append("-");
                }else {
                    String cache = getString(peek);
                    cacheString.append(cache).append("-");
                }
            } else {
                if (length < 5) {
                    //长度小于5  清空缓存
                    cacheString.delete(0, cacheString.length());
                    length = 1;
                }else {
                    if (length >= max) {
                        result = cacheString.toString().substring(0, cacheString.length() - 1);
                        max = length;
                        length = 1;
                    }
                }
            }
            if (cards.size() == 1){
                if (length >= 5 && length >= max) {
                    result = cacheString.toString().substring(0, cacheString.length() - 1);
                    max = length;
                    length = 1;
                }
            }
        }
        return result.length() == 0 ? "NO-CHAIN" : result;

    }

    private static String getString (int pre) {
        String cache = String.valueOf(pre);
        if (pre > 10) {
            switch (pre) {
                case 11:
                    cache = "J";
                    break;
                case 12:
                    cache = "Q";
                    break;
                case 13:
                    cache = "K";
                    break;
                case 14:
                    cache = "A";
                    break;
                default:
                    break;
            }
        }
        return cache;
    }

    private static void reduceCard (String cards) {
        String[] split = cards.split("-");
        for (String s : split) {
            switch (s) {
                case "J":
                    keys.put(11, keys.get(11) - 1);
                    break;
                case "Q":
                    keys.put(12, keys.get(12) - 1);
                    break;
                case "K":
                    keys.put(13, keys.get(13) - 1);
                    break;
                case "A":
                    keys.put(14, keys.get(14) - 1);
                    break;
                default:
                    if (keys.containsKey(Integer.valueOf(s))) {
                        keys.put(Integer.valueOf(s), keys.get(Integer.valueOf(s)) - 1);
                    }
                    break;
            }
        }
    }

}
